﻿using System;
using System.Linq;
using System.Web.Mvc;
using GameSchool.Models.Common;
using GameSchool.Repository;

namespace GameSchool.Controllers
{
    public class NotificationController : Controller       
    {
        IGameSchoolRepository m_data = RepositoryMaster.CurrentRepository;

		public IQueryable<Notification> Notifications()
		{
			return m_data.Notifications;
		}

        // GET: /Default1/
        public ActionResult IndexNotification()
        {
            var model = m_data.Notifications;
            if (model != null) return View(model);
            return null;
        }

        // GET: /Default1/Details/5
        public ActionResult DetailsNotification(int id)
        {
            return View(m_data.Notifications.First(x => x.NotificationId == id));
        }

        // GET: /Default1/Create
        public ActionResult CreateNotification()
        {
        	ViewData["CoursesWhereITeach"] = (from c in m_data.Courses.ToList()
        	                                  select new SelectListItem() {Selected = false, Text = c.CourseName, Value = c.CourseId.ToString()}).ToList();
            return View();
        } 

        // POST: /Default1/Create
        [Authorize(Roles ="Teachers")]
        [HttpPost]
        public ActionResult CreateNotification(Notification notification)
        {

            Models.Person loggedOnPerson = Extra.UserInfo.GetLoggedOnPerson();
            if (loggedOnPerson != null)
            {
                notification.EntryById = loggedOnPerson.PersonId;
                notification.EntryDate = DateTime.Now;
                notification.Active = true;

                m_data.Notifications.Add(notification);
                m_data.SaveChanges();
            }
		   return RedirectToAction("IndexNotification");		   
        }
        
        // GET: /Default1/Edit/5
        public ActionResult EditNotification(int id)
        {            
                var model = m_data.Notifications.Find(id);
                return View(model);           
        }

        // POST: /Default1/Edit/5
        [HttpPost]
        public ActionResult EditNotification(int id, Notification notification)
        {
            try
            {
                Models.Person loggedOnPerson = Extra.UserInfo.GetLoggedOnPerson();

                var item = (from n in m_data.Notifications
                            where n.NotificationId == id
                            select n).SingleOrDefault();

                if (item != null)
				{
                   UpdateModel(item);
                   item.EntryById = loggedOnPerson.PersonId;
                   m_data.SaveChanges();
                   return RedirectToAction("IndexNotification");
				}
                else
                {
					return RedirectToAction("IndexNotification");
                }
            }
            catch
            {
                return View();
            }
        }
    }
}
